<!-- 金币明细 -->
<template>
  <div class="table">
    <div class="container">
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> <i class="el-icon-search"></i> 查询条件： </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-select v-model.trim="searchCondition.type" placeholder="金币分类" clearable class="">
            <el-option v-for="item in selectRedEnvelope" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
          </el-select>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-input v-model.trim="searchCondition.mobile" placeholder="手机号" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-date-picker v-model="queryDate" type="datetimerange" range-separator="至" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="创建时间" @change="changeQueryDate" end-placeholder="结束时间"> </el-date-picker>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-space wrap>
            <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
            <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
          </el-space>
        </el-col>
      </el-row>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item><i class="el-icon-lx-cascades"></i>查询结果：</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-space wrap class="mb20">
        <el-button type="primary" :icon="Download" class="mr10" :disabled="disabledbtn" v-permission="'export'" @click="handleDeriveExcel">导出excel</el-button>
      </el-space>
      <el-table :data="tableList" border class="table" ref="multipleTable" v-loading="loading">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column prop="typeName" label="金币分类" align="center"> </el-table-column>
        <el-table-column prop="mobile" label="手机号" align="center"> </el-table-column>
        <el-table-column prop="money" label="金额" align="center"> </el-table-column>
        <el-table-column prop="create_time" label="时间" align="center"> </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="searchCondition.size"
          :current-page="searchCondition.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :total="total"
          :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="goldInfo">
import { Search, Refresh, Download } from "@element-plus/icons-vue";
import { useDictData } from "@/hooks/network/useDictData";
import { useState } from "@/hooks/useState";
import type { IDictData } from "@/types/dict";
import type { IGoldCoinLog, IGoldCoinLogExport, ISearchGoldCoinLog } from "@/types/moneyManager/goldCoinLog";
import { getGoldLogExportInfo, getGoldLogInfo } from "@/utils/api/goldCoinLog";
const { disabledbtn, loading, toggleState } = useState();
const route = useRoute();

const total = ref(0);
const searchCondition = reactive<ISearchGoldCoinLog>({
  type: "",
  mobile: "",
  start_create_time: "",
  end_create_time: "",
  current: 1,
  size: 20
});
const tableList = ref<Array<IGoldCoinLog>>([]);
const queryDate = ref<Array<string>>([]);

const selectRedEnvelope = ref<Array<IDictData>>([]);

/* 查询 创建时间更改事件 */
function changeQueryDate(data: string[]) {
  if (data !== null) {
    searchCondition.start_create_time = data[0];
    searchCondition.end_create_time = data[1];
  } else {
    searchCondition.start_create_time = "";
    searchCondition.end_create_time = "";
  }
}
/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 20;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.type = "";
  searchCondition.mobile = "";
  searchCondition.start_create_time = "";
  searchCondition.end_create_time = "";
  queryDate.value = [];
  searchInfo();
}
/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.size = val;
  getTableInfo();
}

/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getGoldLogInfo<IGoldCoinLog, ISearchGoldCoinLog>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}
/* 导出excel */
function handleDeriveExcel() {
  toggleState(true);
  /* 可以前端增加 筛选条件 */
  getGoldLogExportInfo<IGoldCoinLogExport, ISearchGoldCoinLog>(searchCondition)
    .then(res => {
      window.open(import.meta.env.VITE_API_URL + res.result.url, "_blank");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}

let { getDictData } = useDictData();
getDictData("GOLD_USE_TYPE", selectRedEnvelope);
onMounted(() => {
  if (route.query.mobile == undefined) {
    getTableInfo();
  }
});
onActivated(() => {
  if (route.query.mobile !== undefined) {
    if (route.query.mobile !== searchCondition.mobile) {
      searchCondition.mobile = route.query.mobile as string;
      getTableInfo();
    }
  }
});
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
